<?
/**
 * generic.php
 *
 * <pre>
 * Devyra Internacionalnaya
 * Vebba PHP Framework and Website Builder
 * Extended functions for PHP
 * </pre>
 *
 * @package   --
 * @version   0.7.0303 alpha
 * @since     2012
 * @author    Paul Dinh
 * @copyright Devyra Intenacionalnaya
 */

/**
 * Print out skin name
 */ 
function skin() {
  echo system::get_skin_name();
}

/**
 * Get value in an array by a key
 *
 * @param  array $Array The array to get value out
 * @param  mixed $Key   The key to find value
 * @return mixed        The value in array
 */
function val($Array,$Key) {
  if (!array_key_exists($Key,$Array))
    return null;
  else
    return $Array[$Key];
}

/**
 * Handle php shutdown to log the fatal errors
 */
function handle_shutdown() {
  $Error = error_get_last();
  if ($Error!=null) {
    $Err_No   = $Error["type"];
    $Err_Msg  = $Error["message"];
    $Err_File = $Error["file"];
    $Err_Line = $Error["line"];
    handle_error($Err_No,$Err_Msg,$Err_File,$Err_Line);
  }
}
 
/**
 * Handle errors when they happen
 */ 
function handle_error($Err_No,$Err_Msg,$Err_File,$Err_Line) {
  break_all_buffers();
  echo "<hr/><pre style='text-align:left;'>";
  
  //get function call backtrace
  ob_start();
  debug_print_backtrace();
  $Trace = ob_get_clean();
  
  //info
  echo "Error $Err_No:\r\n$Err_Msg\r\n\r\n";
  echo "In file:\r\n$Err_File\r\n\r\n";
  echo "At line:\r\n$Err_Line\r\n\r\n";
  echo "Backtrace:\r\n$Trace";
  
  echo "<hr/></pre>";  
  exit(-1);
}
 
/**
 * Print out a phrase translation
 */ 
function __($Phrase_Const,$Ucfirst=1) {
  echo phrase::__($Phrase_Const,$Ucfirst);
}
 
/**
 * Break all output buffers
 */ 
function break_all_buffers() {
  while (ob_get_level()>0)
    ob_end_clean();
}
 
/**
 * Dump out stack backtrace
 */ 
function dump_backtrace() {
  echo "<hr/><pre style='text-align:left;'>";
  ob_start();
  debug_print_backtrace();
  ob_end_flush();
  echo "<hr/></pre>";   
}
 
/**
 * Dump variable to browser
 */
function dump($Var) {
  echo "<hr/><pre style='text-align:left;'>";
  var_dump($Var);
  echo "<hr/></pre>";  
}

/**
 * Show error message
 */ 
function show_err($Error_Msg) {
  echo "<hr/><pre style='text-align:left;'>";
  echo $Error_Msg;
  echo "<hr/></pre>";  
}